﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AttackController : MonoBehaviour
{
    public thisController player;
    public float shootSpeed = 0.5f;
    public float shootDuration = 5.0f;
    public GameObject bullet;
    public Transform gunBase;
    public ShakeCamera mainCamera;
    
    public Transform[] gunPositions = new Transform[3];
    private float shootTime = 0.0f;

    private void Update()
    {
        GunDirection();

        float shootTimeInterval = 1 / shootSpeed;
        shootTime += Time.deltaTime;
        if (shootTime > shootTimeInterval)
        {
            shootTime -= shootTimeInterval;
            Shoot();
        }
    }

    private void GunDirection()
    {
        if (player.transform.localScale.x < 0)
        {
            gunBase.transform.forward = new Vector3(0, 0, -180);
        }

        if (player.transform.localScale.x > 0)
        {
            gunBase.transform.forward = new Vector3(0, 0, 0);
        }

    }

    private void Shoot()
    {
        if(Input.GetKey(KeyCode.J))
        {
            foreach(Transform _gunPos in gunPositions)
            {
                if(_gunPos !=null)
                {
                    GameObject clone = ObjectPool.Instance.OutPool(bullet);
                    clone.transform.position = _gunPos.position;
                    clone.transform.rotation = _gunPos.rotation;
                    AudioController.Play("playergun");
                }
            }
            mainCamera.shake();
        }

    }


}
